<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-tabs defaultActiveKey="1" @change="callback">
          <a-tab-pane tab="审批" key="1">

            <a-form-item label="受理人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input   v-decorator="[ 'operator', validatorRules.operator]" placeholder="请输入受理人"></a-input>
            </a-form-item>
            <a-form-item label="回复内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea  v-decorator="['replyContent', validatorRules.replyContent]" rows="4" placeholder="请输入回复内容"/>
            </a-form-item>
            <a-form-item label="相关资料" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--              <j-upload   v-decorator="['information', validatorRules.information]" :trigger-change="true"></j-upload>-->
             <j-upload-img v-decorator="['information', validatorRules.information]" :trigger-change="true"></j-upload-img>
            </a-form-item>
            <!--        0_通过，1_拒绝-->
            <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag :disabled="isEdit" type="list" v-decorator="['state', validatorRules.state]" :trigger-change="true" dictCode="process_state"
                                 placeholder="请选择状态"/>
            </a-form-item>
            <div style="text-align: center" v-if="isShowGongShi">
              <a @click="gongShi()">
                <a-button type="primary" icon="plus">添加数据到公示台账</a-button>
              </a>
            </div>
            <div style="text-align: center" v-if="isShowTuiJian">
              <a @click="tuiJian()">
                <a-button type="primary" icon="plus">添加数据到推荐台账</a-button>
              </a>
            </div>
            <div style="text-align: center" v-if="isShowFangKuan">
              <a @click="fangKuan()">
                <a-button type="primary" icon="plus">添加数据到放款台账</a-button>
              </a>
            </div>
          </a-tab-pane>
          <a-tab-pane tab="流程" key="2" forceRender>
            <img src="../../../../assets/liucheng.png" style="margin-top:50px" alt="logo">
            <div>
            </div>
          </a-tab-pane>
        </a-tabs>
      </a-form>
    </a-spin>
    <SmallAmountPublicityAccountModal ref="smallAmountPublicityAccountModal"></SmallAmountPublicityAccountModal>
    <SmallLoanAccountModal ref="smallLoanAccountModal"></SmallLoanAccountModal>
    <SmallRecommendationAccountModal ref="smallRecommendationAccountModal"></SmallRecommendationAccountModal>

  </a-modal>
</template>

<script>
    import {httpAction, getAction} from '@/api/manage'
    import pick from 'lodash.pick'
    import JDate from '@/components/jeecg/JDate'
    import JUpload from '@/components/jeecg/JUpload'
    import JDictSelectTag from "@/components/dict/JDictSelectTag"
    import SmallAmountPublicityAccountModal from "./SmallAmountPublicityAccountModal";
    import SmallLoanAccountModal from "./SmallLoanAccountModal";
    import SmallRecommendationAccountModal from "./SmallRecommendationAccountModal";
    import ProcessingListModal from "./ProcessingListModal";
    import ProcessFinishListModal from "./ProcessFinishListModal";
    import JUploadImg from "../../../../components/jeecg/JUploadImg";
    export default {
        name: "ProcessingModal",
        components: {
            JUploadImg,
            ProcessFinishListModal,
            ProcessingListModal,
            SmallRecommendationAccountModal,
            SmallLoanAccountModal,
            SmallAmountPublicityAccountModal,
            JDate,
            JUpload,
            JDictSelectTag,
        },
        data() {
            return {
                isShowFinish:false,
                isShow:false,
                paramProcessId:"",
                state:"",
                isEdit:false,
                isShowGongShi: false,
                isShowTuiJian: false,
                isShowFangKuan: false,
                pass: true,
                form: this.$form.createForm(this),
                title: "审批",
                width: 800,
                visible: false,
                model: {},
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },
                confirmLoading: false,
                validatorRules: {
                    processId: {},
                    approvalProcessType: {},
                    approvalProcessCode: {},
                    approvalProcessDate: {},
                    operator: {rules: [{ required: true, message: '受理人不能为空'}]},
                    replyContent: {rules: [{ required: true, message: '回复内容不能为空'}]},
                    information: {},
                    state: {rules: [{ required: true, message: '状态不能为空'}]},
                },
                url: {
                    add: "/loanmanagement/processing/add",
                    edit: "/loanmanagement/processing/edit",
                    editFinish: "/loanmanagement/processFinish/edit",
                    judgeAuditState: "/loanmanagement/processing/getProcessNodeDateByProcessIdAndOrgCode",
                    queryById:"/loanmanagement/poorHouseholdLoan/queryById",
                }
            }
        },
        created() {
        },
        methods: {
            handleChange({ fileList }) {
                this.fileList = fileList;
                var name=this.fileList[this.fileList.length-1].response.message;
                this.fileList[this.fileList.length-1].thumbUrl="/jeecg-boot/sys/common/view/"+name;
                this.fileList[this.fileList.length-1].url="/jeecg-boot/sys/common/view/"+name;
                console.log(JSON.stringify(this.fileList[this.fileList.length-1]))
            },
            gongShi() {
                var processId= this.model.processId;
                this.$refs.smallAmountPublicityAccountModal.showGoodsGallery1(processId);
            },
            tuiJian() {
                var processId= this.model.processId;
                this.$refs.smallRecommendationAccountModal.showGoodsGallery1(processId);
            },
            fangKuan() {
                var processId= this.model.processId;
                this.$refs.smallLoanAccountModal.showGoodsGallery1(processId);
            },
            callback(key) {
                // if (key == "3") {
                //     var record={};
                //     record.id=this.paramProcessId;
                //     getAction(this.url.queryById, record).then((res) => {
                //         if (res.success) {
                //             record.state=res.result.state;
                //             if (record.state == 1) {
                //                 this.$refs.processingListModal.showGoodsGallery1(record);
                //             } else {
                //                 this.$refs.processFinishListModal.showGoodsGallery1(record);
                //             }
                //         }
                //         this.loading = false;
                //     })
                // }
            },
            add() {
                this.edit({});
            },
            edit(record) {
                this.form.resetFields();
                this.model = Object.assign({}, record);
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model, 'processId', 'approvalProcessType', 'approvalProcessCode', 'approvalProcessDate', 'operator', 'replyContent', 'information', 'state'))
                })
            },
            close() {
                this.$emit('close');
                this.visible = false;
            },
            handleOk() {
                const that = this;
                // if (this.pass) {
                    // 触发表单验证
                    this.form.validateFields((err, values) => {
                        if (!err) {
                            that.confirmLoading = true;
                            let httpurl = '';
                            let method = '';
                            if (!this.model.id) {
                                httpurl += this.url.add;
                                method = 'post';
                            } else {
                                if (this.state == '1') {
                                    httpurl += this.url.edit;
                                } else{
                                    httpurl += this.url.editFinish;
                                }
                                method = 'put';
                            }
                            let formData = Object.assign(this.model, values);
                            console.log("表单提交数据", formData)
                            httpAction(httpurl, formData, method).then((res) => {
                                if (res.success) {
                                    that.$message.success(res.message);
                                    that.$emit('ok');
                                } else {
                                    that.$message.warning(res.message);
                                }
                            }).finally(() => {
                                that.confirmLoading = false;
                                that.close();
                                this.$emit('parentComponentReLoad', 'AA')
                            })
                        }
                    })
                that.close();
            },
            handleCancel() {
                this.close();
            },
            popupCallback(row) {
                this.form.setFieldsValue(pick(row, 'processId', 'approvalProcessType', 'approvalProcessCode', 'approvalProcessDate', 'operator', 'replyContent', 'information', 'state'))
            },
            setTitle(res) {
                if (res.result.approvalProcessCode != 'false') {
                    if (res.result.approvalProcessCode == '0') {
                        this.title = "贫困户申请"
                    } else if (res.result.approvalProcessCode == '1') {
                        this.title = "银行带钱调查"
                    }
                    else if (res.result.approvalProcessCode == '2') {
                        this.title = "村级公示"
                    } else if (res.result.approvalProcessCode == '3') {
                        this.title = "乡镇公示"
                        this.isShowGongShi = true;
                    } else if (res.result.approvalProcessCode == '4') {
                        this.title = "县推荐"
                        this.isShowTuiJian = true;
                    } else if (res.result.approvalProcessCode == '5') {
                        this.title = "银行受理"
                    } else if (res.result.approvalProcessCode == '6') {
                        this.title = "签订合同"
                    }else if (res.result.approvalProcessCode == '7') {
                        this.title = "发放贷款"
                        this.isShowFangKuan = true;
                    }
                }
            },
            insertPageOrNotPermissions(res,record){
                if (res.result.approvalProcessCode == 'false') {
                    //  提示不能审批
                    if (record.state == '1') {
                        this.$message.warning("还没有到你审批，不要着急");
                    } else if (record.state == '2') {
                        this.$message.warning("此次申请已被拒绝，不用审批");
                    }
                } else {
                    this.isEdit=false;
                    this.pass=true;
                    this.$message.success("新增页面");
                    var recordCopy = {};
                    recordCopy.approvalProcessType = '0';
                    recordCopy.approvalProcessCode = res.result.approvalProcessCode;
                    recordCopy.processId = record.id;
                    this.edit(recordCopy);
                    this.visible = true
                }
            },
            dataEcho(res){
                this.edit(res.result);
                if (res.message == "数据回显") {
                    this.$message.success("数据回显");
                    this.pass = false;
                    if (res.result.state != "1") {
                        this.isEdit=true;
                    }
                }else{
                    this.isEdit=false;
                    this.pass=true;
                    this.$message.success("新增页面");
                }
                this.visible = true
            },
            showGoodsGallery1(record) {
                //如果审核过，则显示审核信息，审核通过则隐藏确认和取消按钮；
                //如果没有审核过，则为添加数据
                var params = {};
                params.processId = record.id;
                this.paramProcessId=record.id;
                params.state = record.state;
                this.state=record.state;
                getAction(this.url.judgeAuditState, params).then((res) => {
                    if (res.message == "没有权限") {
                        this.$message.warning("没有权限");
                        return;
                    }
                    this.setTitle(res);
                    if (res.code === 500) {
                        this.insertPageOrNotPermissions(res,record);
                    } else if (res.success) {
                        res.result.processId=record.id;
                        this.dataEcho(res);
                    }
                    this.loading = false;
                })
            }
        }
    }
</script>
<style scoped>
  /* tile uploaded pictures */
  .upload-list-inline >>> .ant-upload-list-item {
    float: left;
    width: 200px;
    margin-right: 8px;
  }
  .upload-list-inline >>> .ant-upload-animate-enter {
    animation-name: uploadAnimateInlineIn;
  }
  .upload-list-inline >>> .ant-upload-animate-leave {
    animation-name: uploadAnimateInlineOut;
  }
</style>